package com.bamboo.core.exceptions;

import com.bamboo.core.domain.response.R;
import com.bamboo.core.enums.code.CodeCommonEnum;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.support.DefaultMessageSourceResolvable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.validation.ConstraintViolation;
import javax.validation.ConstraintViolationException;
import java.util.stream.Collectors;

/**
* @description 
* @date 2025/3/7 17:41
* @author chenzh
*/
@RestControllerAdvice
public class GlobalExceptionHandler {
    final Logger logger = LoggerFactory.getLogger(GlobalExceptionHandler.class);

    /**
     * @description 处理参数校验失败触发的异常
     * @date 2025/3/18 8:55
     * @author chenzh
     * @param exception 参数校验失败的异常信息
     * @return ResponseEntity<R<?>>
     */
    @ExceptionHandler(MethodArgumentNotValidException.class)
    public ResponseEntity<R<?>> handleException(MethodArgumentNotValidException exception) {
        // 提取所有的错误信息
        String errMsg = exception.getAllErrors().stream().map(DefaultMessageSourceResolvable::getDefaultMessage)
                .collect(Collectors.joining(","));
        return new ResponseEntity<>(R.fail(CodeCommonEnum.INVALID_ARG.getCode(), errMsg), HttpStatus.OK);
    }

    /**
     * @description 接口需要请求体参数，但是请求体为空导致的异常
     * @date 2025/3/18 8:55
     * @author chenzh
     * @return ResponseEntity<R<?>>
     */
    @ExceptionHandler(HttpMessageNotReadableException.class)
    public ResponseEntity<R<?>> handleException() {
        return new ResponseEntity<>(R.fail(CodeCommonEnum.EMPTY_BODY), HttpStatus.OK);
    }

    /**
     * @description 处理校验级联参数失败的异常
     * @date 2025/3/18 10:11
     * @author chenzh
     * @param exception 异常信息
     * @return ResponseEntity<R<?>>
     */
    @ExceptionHandler(ConstraintViolationException.class)
    public ResponseEntity<R<?>> handleException(ConstraintViolationException exception) {
        String msg = exception.getConstraintViolations()
                .stream()
                .map(ConstraintViolation::getMessage)
                .collect(Collectors.joining(";"));
        return new ResponseEntity<>(R.fail(CodeCommonEnum.INVALID_ARG.getCode(), msg), HttpStatus.OK);
    }


    /**
     * @description 处理框架定义的异常
     * @date 2025/3/18 8:55
     * @author chenzh
     * @param exception BambooException
     * @return ResponseEntity<R<?>>
     */
    @ExceptionHandler(BambooException.class)
    public ResponseEntity<R<?>> handleException(BambooException exception) {
        return new ResponseEntity<>(R.fail(exception.getCode(), exception.getMessage()), HttpStatus.OK);
    }

    @ExceptionHandler(Exception.class)
    public ResponseEntity<R<?>> handleException(Exception exception) {
        logger.error("", exception);
        return new ResponseEntity<>(R.fail(exception.getMessage()), HttpStatus.OK);
    }
}
